package com.sl.au.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Service;

import com.sl.au.entity.VisitorReward;
import com.sl.au.repository.VisitorRewardRepository;
import com.sl.au.service.VisitorRewardService;
@Service
@Transactional
public class VisitorRewardServiceImpl extends BaseServiceImpl implements VisitorRewardService{
	
	@Autowired
	VisitorRewardRepository visitorRewardRepository;
	@Resource
	private NamedParameterJdbcTemplate namedParameterJdbcTemplate;
	
	@Override
	public VisitorRewardRepository getRepository() {
		return this.visitorRewardRepository;
	}

	@Override
	public int getUserSameCycleAndTypeCount(String userId, String registerId, Date begin, Date end) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("begin", begin);
		params.put("end", end);
		params.put("userId", userId);
		params.put("registerId", registerId);
		int count = 0;
		String SQL = "select count(1) from sl_wcsi_visitor_reward a left join sl_wcsi_EventRegister b on a.registerId = b.id "
				+ " left join sl_wcsi_EventReport c on b.registerEventToReportEvent = c.id "
				+ " where a.userId = :userId and c.reportAccTime >:begin and c.reportAccTime <:end "
				//同一事件类型
				+ " and b.registerEventType = (select d.registerEventType from sl_wcsi_EventRegister d where d.id = :registerId) "
				//同一网格
				+ " and c.reportEventToNetGrid = (select f.reportEventToNetGrid from sl_wcsi_EventRegister e left join sl_wcsi_EventReport f on e.registerEventToReportEvent = f.id where e.id = :registerId) ";
		count = namedParameterJdbcTemplate.queryForObject(SQL,params,Integer.class);
		return count;
	}

	@Override
	public List<VisitorReward> findRewardByKeyAndCon(int start, int length, String key, String con) {
		Pageable pageable=new PageRequest(start/length, length,Sort.Direction.DESC,"createTime");
		return visitorRewardRepository.findRewardByKeyAndCon(pageable,key,con).getContent();
	}

	@Override
	public int findRewardCountByKeyAndCon(String key, String con) {
		Pageable pageable=new PageRequest(0, Integer.MAX_VALUE,Sort.Direction.DESC,"createTime");
		return visitorRewardRepository.findRewardByKeyAndCon(pageable,key,con).getContent().size();
	}

	@Override
	public List<VisitorReward> findRewardOnlyKey(int start, int length, String key) {
		Pageable pageable=new PageRequest(start/length, length,Sort.Direction.DESC,"createTime");
		return visitorRewardRepository.findRewardOnlyKey(pageable,key).getContent();
	}

	@Override
	public int findRewardCountOnlyKey(String key) {
		Pageable pageable=new PageRequest(0, Integer.MAX_VALUE,Sort.Direction.DESC,"createTime");
		return visitorRewardRepository.findRewardOnlyKey(pageable,key).getContent().size();
	}

	@Override
	public double getUserRewardByDate(String userId, Date begin, Date end) {
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("begin", begin);
		params.put("end", end);
		params.put("userId", userId);
		double reward = 0;
		String SQL = "select ifnull(sum(a.rewardValue),0) from sl_wcsi_visitor_reward a "
				+ " where a.userId = :userId and a.createTime >:begin and a.createTime <:end ";
		reward = namedParameterJdbcTemplate.queryForObject(SQL,params,Double.class);
		return reward;
	}

}
